package com.caotv.wkreport.utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.caotv.wkreport.dao.Bookingroom;
import com.caotv.wkreport.dao.Rooms;
import com.caotv.wkreport.dbutils.BookingRoomTableTool;

public class CommonUtils {

	public static boolean checkBookedRoom(Date startDate, Date endDate,
			int roomId) {
		List<Bookingroom> listBooked = new ArrayList<Bookingroom>();
		listBooked = BookingRoomTableTool.getInstance().getBookingRoomInfo(
				roomId);
		for (Bookingroom bookingroom : listBooked) {

			Bookingroom bookingObj = (Bookingroom) bookingroom;
			boolean valid = false;
			System.out.print("Start time:"
					+ bookingObj.getStartTime().toString());
			System.out.print("End time:" + bookingObj.getEndTime().toString());
			if (bookingObj.getStartTime().after(endDate)
					|| bookingObj.getEndTime().before(startDate)) {
				valid = true;
			}
			if (!valid) {
				return false;
			}
		}
		return true;
	}

	public static List<BookingMatrix> fillBookingMatrix(List<Rooms> listRooms,
			List<Bookingroom> listBookingRooms, Date bookingDate) {
		List<BookingMatrix> listBookingMatrix = new ArrayList<BookingMatrix>();
		for (int i = 9; i < 18; i++) {
			for (int j = 0; j < 60; j += 30) {
				bookingDate.setHours(i);
				bookingDate.setMinutes(j);
				BookingMatrix matrixRow = getRow(listBookingRooms, listRooms,
						bookingDate);
				listBookingMatrix.add(matrixRow);
			}
		}
		return listBookingMatrix;
	}
	
	public static List<BookingMatrix> fillBookingInfoMatrix(int roomID,
			List<Bookingroom> listBookingRooms, List<Date> dates) {
		List<BookingMatrix> listBookingMatrix = new ArrayList<BookingMatrix>();
		for(int t=0;t<dates.size();t++){
		    Date lDate =(Date)dates.get(t);    
	    
		    BookingMatrix matrixRow = getRowInfo(listBookingRooms, lDate);
		    listBookingMatrix.add(matrixRow);
	}
		return listBookingMatrix;
	}
	
	private static BookingMatrix getRowInfo(List<Bookingroom> listBookingRoom,	Date startTime) {
		BookingMatrix bookingMatrix = new BookingMatrix();
		String[] row = new String[18];
		for (int i = 0; i < row.length; i++) {
			row[i] = "availble";
		}

	    for (int i = 9; i < 18; i++) {
	    	for (int j = 0; j < 60; j += 30) {
	    		startTime.setHours(i);
	    		startTime.setMinutes(j);
	    
		DecimalFormat nft = new DecimalFormat("#00.###");
		nft.setDecimalSeparatorAlwaysShown(false);
		Date endTime = addTimeToDate(startTime, 30);
		String timeZone = startTime.getHours() + ":"
				+ nft.format(startTime.getMinutes()) + "-" + endTime.getHours()
				+ ":" + nft.format(endTime.getMinutes());
		bookingMatrix.setTimeZone(timeZone);

		for (Bookingroom bookingroom : listBookingRoom) {
			if (bookingroom.getStartTime().getTime() <= startTime.getTime()
					&& bookingroom.getEndTime().getTime() >= endTime.getTime()) {
				int column = 0;
				if(j==0) {
					column = 2*(i - 9);
				}
				else {
					column = 2*(i - 9) +1;				
				}
				row[column] = "booked";
			}
		}

		
	    	}
	    }
		
		
		bookingMatrix.setRow(row);
		return bookingMatrix;
	}
	
	
	
	private static BookingMatrix getRow(List<Bookingroom> listBookingRoom,
			List<Rooms> listRooms, Date startTime) {
		BookingMatrix bookingMatrix = new BookingMatrix();
		String[] row = new String[listRooms.size()];
		for (int i = 0; i < row.length; i++) {
			row[i] = "availble";
		}

		DecimalFormat nft = new DecimalFormat("#00.###");
		nft.setDecimalSeparatorAlwaysShown(false);
		Date endTime = addTimeToDate(startTime, 30);
		String timeZone = startTime.getHours() + ":"
				+ nft.format(startTime.getMinutes()) + "-" + endTime.getHours()
				+ ":" + nft.format(endTime.getMinutes());
		bookingMatrix.setTimeZone(timeZone);

		for (Bookingroom bookingroom : listBookingRoom) {
			if (bookingroom.getStartTime().getTime() <= startTime.getTime()
					&& bookingroom.getEndTime().getTime() >= endTime.getTime()) {
				int roomIndex = findRoomIndex(listRooms, bookingroom.getRooms());
				row[roomIndex] = "booked";
			}
		}

		bookingMatrix.setRow(row);
		return bookingMatrix;
	}

	private static int findRoomIndex(List<Rooms> listRooms, Rooms room) {
		for (int i = 0; i < listRooms.size(); i++) {
			if (listRooms.get(i).getId() == room.getId()) {
				return i;
			}
		}
		return -1;
	}

	public static Date addTimeToDate(Date date, int minitures) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minitures);
		return calendar.getTime();
	}
}
